﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public class BoardUtil
	{
		private BoardUtil() { }

		public static String GetAttachmentFileDownloadUrl(String componentID, String fileID)
		{
			if (componentID == null)
			    throw new ArgumentNullException("componentID");

			if (fileID == null)
			    throw new ArgumentNullException("fileID");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(fileID))
				throw new ArgumentOutOfRangeException("fileID");

			if (!VUtility.IsGuid(fileID))
				throw new FormatException("fileID must Guid.");

			return String.Format(VUtility.WebApplicationVirtualPath + "vboard/component/board/handler/download.ashx?cid={0}&fid={1}", componentID, fileID);
		}

		public static String GetAttachmentFileViewUrl(String componentID, String fileID)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (fileID == null)
				throw new ArgumentNullException("fileID");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(fileID))
				throw new ArgumentOutOfRangeException("fileID");

			if (!VUtility.IsGuid(fileID))
				throw new FormatException("fileID must Guid.");

			return String.Format(VUtility.WebApplicationVirtualPath + "vboard/component/board/handler/view.ashx?cid={0}&fid={1}", componentID, fileID);
		}

		public static String GetAttachmentThumbnailUrl(String componentID, String fileID)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (fileID == null)
				throw new ArgumentNullException("fileID");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(fileID))
				throw new ArgumentOutOfRangeException("fileID");

			if (!VUtility.IsGuid(fileID))
				throw new FormatException("fileID must Guid.");

			return String.Format(VUtility.WebApplicationVirtualPath + "vboard/component/board/handler/thumbnail.ashx?cid={0}&fid={1}", componentID, fileID);
		}

		public static String GetBoardCategoryIconUrl(String componentID, String categoryID)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (!String.IsNullOrEmpty(categoryID) && !VUtility.IsGuid(categoryID))
				throw new FormatException("categoryID must Guid.");

			return String.Format(VUtility.WebApplicationVirtualPath + "vboard/component/board/handler/categoryicon.ashx?cid={0}&cate={1}", componentID, categoryID);
		}

		public static String GetBoardUrl(BoardPart part, Int32 pageIndex, String categoryID, String threadID, String searchText, Int32 x, Int32 y)
		{
			if (!String.IsNullOrEmpty(categoryID) && !VUtility.IsGuid(categoryID))
				throw new FormatException("categoryID must Guid.");

			if (!String.IsNullOrEmpty(threadID) && !VUtility.IsGuid(threadID))
				throw new FormatException("threadID must Guid.");

			return GetBoardUrl(HttpContext.Current.Request.Path, part, pageIndex, categoryID, threadID, searchText, x, y);
		}

		public static String GetBoardUrl(String pageUrl, BoardPart part, Int32 pageIndex, String categoryID, String threadID, String searchText, Int32 x, Int32 y)
		{
			if (!String.IsNullOrEmpty(categoryID) && !VUtility.IsGuid(categoryID))
				throw new FormatException("categoryID must Guid.");

			if (!String.IsNullOrEmpty(threadID) && !VUtility.IsGuid(threadID))
				throw new FormatException("threadID must Guid.");

			pageUrl = pageUrl.Replace("~/", VUtility.WebApplicationVirtualPath);//버그번호2009020501에 의해 추가된 코드

			String url = String.Format(pageUrl + "?part={0}&pindex={1}&cate={2}&tid={3}&search={4}&x={5}&y={6}", part, pageIndex, categoryID, threadID, HttpContext.Current.Server.UrlEncode(searchText), x, y);

			foreach (String key in HttpContext.Current.Request.QueryString.AllKeys)
			{
				if (!isReservationQueryString(key))
				{
					url += String.Format("&{0}={1}", key, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.QueryString[key]));
				}
			}

			return url;
		}

		private static Boolean isReservationQueryString(String key)
		{
			Boolean resultFlag = false;

			String[] reservationKeys = new String[] { "cid", "part", "pindex", "cate", "tid", "search", "fid", "x", "y" };

			foreach (String reservationKey in reservationKeys)
			{
				if (reservationKey.ToLower() == key.ToLower())
				{
					resultFlag = true;
					break;
				}
			}

			return resultFlag;
		}

		public static String GetBoardSearchResultUrl(String searchText)
		{
			String url = String.Format(HttpContext.Current.Request.Path + "?search={0}", HttpContext.Current.Server.UrlEncode(searchText));

			foreach (String key in HttpContext.Current.Request.QueryString.AllKeys)
			{
				if (!isReservationQueryString(key))
				{
					url = url + String.Format("&{0}={1}", key, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.QueryString[key]));
				}
			}

			return url;
		}
	}
}
